package com.njbw.design_pattern.created.builder;

/**
 * HTML 文档类
 *
 * @author 黄侯淳
 * @since JDK 1.8
 */
public class HtmlDocument {

    /**
     * 提供很多属性，有的是可选项，有的是必选项
     */
    // 文档的头部
    private String header = "";
    // 文档的主体
    private String body = "";
    // 文档的尾部
    private String footer = "";

    public void addHeader(String header) {
        this.header = header;
    }

    public void addBody(String body) {
        this.body = body;
    }

    public void addFooter(String footer) {
        this.footer = footer;
    }

    @Override
    public String toString() {
        return "<html>\n" +
                "<head>\n" +
                header +
                "</head>\n" +
                "<body>\n" +
                body +
                "</body>\n" +
                "<footer>\n" +
                footer +
                "</footer>\n" +
                "</html>";
    }

    public static class Builder {

        private final HtmlDocument document;

        public Builder() {
            document = new HtmlDocument();
        }

        public Builder addHeader(String header) {
            document.addHeader(header);
            return this;
        }

        public Builder addBody(String body) {
            document.addBody(body);
            return this;
        }

        public Builder addFooter(String footer) {
            document.addFooter(footer);
            return this;
        }

        public HtmlDocument build() {
            return document;
        }
    }
}
